//
//  BaseTabBarController.m
//  Rongniu
//
//  Created by Apple on 16/2/22.
//  Copyright © 2016年 Fangwei. All rights reserved.
//

#import "BaseTabBarController.h"
#import "BaseNavigatinController.h"

#import "HomeViewController.h"
#import "AllProjectController.h"
#import "GoingProjectViewController.h"
#import "MineViewController.h"
#import "XHF_SMSLoginViewController.h"


@interface BaseTabBarController ()

@property (nonatomic ,strong)ZhuangxiuManager *manager;
@end

@implementation BaseTabBarController

#pragma mark - life cycle

- (void)viewDidLoad {
    
    
    [super viewDidLoad];
    //1、创建viewCtrls
    [self createViewCtrls];
    
    //2、自定义uitabbar
    [self customTabBar];
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self removeButton];
    
    
    
}
-(void)createViewCtrls{
 
    BaseNavigatinController *home = [[BaseNavigatinController alloc]initWithRootViewController:[[HomeViewController alloc]init]];
    BaseNavigatinController *going = [[BaseNavigatinController alloc]initWithRootViewController:  [[GoingProjectViewController alloc]init]];
    BaseNavigatinController *allProject = [[BaseNavigatinController alloc]initWithRootViewController:[[AllProjectController alloc]init]];
    BaseNavigatinController *mine = [[BaseNavigatinController alloc]initWithRootViewController:[[MineViewController alloc]init]];
    
    NSArray *viewCtrls = @[
                          home,
                          going,
                          allProject,
                          mine
 
                           
                           ];
    
    self.viewControllers = viewCtrls;
}

-(void) customTabBar{
    
    //背景
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"caidan"]];
    
    //按钮的宽度
    CGFloat buttonWidth = SCREEN_WIDTH/4.0;
    
    
    NSArray *imageoffArray= @[
                              @"首页",
                              @"工作报告",
                              @"项目",
                              @"个人中心",
                              ];
    
    
    NSArray *imageonArray = @[
                              @"首页-2",
                              @"工作报告-2",
                              @"项目-2",
                              @"个人中心-2",
                              ];
    
    for (int i = 0; i < imageoffArray.count; i++)
    {
        
        
        //每个按钮的frame
        CGRect frame = CGRectMake(buttonWidth * i, 0, buttonWidth, 49);
        UIButton *button = [[UIButton alloc]initWithFrame:frame];
        [button setImage:[UIImage imageNamed:imageoffArray[i]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:imageonArray[i]] forState:UIControlStateSelected];
        button.tag = 100 + i;
        [button addTarget:self
                   action:@selector(buttonAction:)
         forControlEvents:UIControlEventTouchUpInside];
        
        [self.tabBar addSubview:button];
    }
    
    //设置默认第一个
    UIButton *bt1 =   (UIButton *)[self.view viewWithTag:100];
    bt1.selected = YES;
    
    
    
    
    
}


#pragma mark - event response

-(void) removeButton{
    
    for (UIView *view in self.tabBar.subviews)
    {
        //从字符串生成一个类名
        Class class = NSClassFromString(@"UITabBarButton");
        
        //找出UITabBarButton，移除掉
        if ([view isKindOfClass:class])
        {
            [view removeFromSuperview];
        }
        
    }
    
    
}

//按钮的点击事件
- (void)buttonAction:(UIButton *)button {
    _manager = [ZhuangxiuManager shareManager];

    
    if (button.tag-100 == 1||button.tag-100 == 2) {
        
        if ([_manager.loginSucceed integerValue]>0) {
            
            self.selectedIndex = button.tag-100;
        }
        else{
            
            [self presentLogin];
            return;
            
        }
        
    }else{
        
        
        self.selectedIndex = button.tag-100;
        
        
    }
    UIButton *bt1 =  (UIButton *)[self.view viewWithTag:100];
    UIButton *bt2 =  (UIButton *)[self.view viewWithTag:101];
    UIButton *bt3 =  (UIButton *)[self.view viewWithTag:102];
    UIButton *bt4 =  (UIButton *)[self.view viewWithTag:103];
    
    bt1.selected = NO;
    bt2.selected = NO;
    bt3.selected = NO;
    bt4.selected = NO;
    switch (button.tag-100) {
        case 0:
            bt1.selected = YES;
            break;
        case 1:
            bt2.selected = YES;
            break;
        case 2:
            bt3.selected = YES;
            break;
        case 3:
            bt4.selected = YES;
            break;
        default:
            break;
    }
    
//    //切换控制器
//    self.selectedIndex = button.tag-100;
    
    
    
}

-(void)presentLogin{

    XHF_SMSLoginViewController *loginCtrl = [[XHF_SMSLoginViewController alloc]init];
    loginCtrl.logintype = LOGINTYPEOTERS;
    UINavigationController *nav = [[BaseNavigatinController alloc] initWithRootViewController:loginCtrl];

    [self presentViewController:nav animated:YES completion:NULL];


}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
